package com.yupi.yuaiagent.rag;

import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class HappyAppVectorStoreConfig {

    @Resource
    HappyAppDocumentLoader happyAppDocumentLoader;

    @Resource
    MyTokenTextSplitter myTokenTextSplitter;

    @Resource
    MyKeywordEnricher myKeywordEnricher;

    @Bean
    VectorStore happyAppVectorStore(@Qualifier("dashscopeEmbeddingModel") EmbeddingModel embeddingModel) {
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(embeddingModel)
                .build();
        // 读取本地情报专家的文档
        List<Document> documents = happyAppDocumentLoader.loadMarkdownDocuments();

        // 自主分割文档，建议使用云平台的智能切分加手工调整
        List<Document> splitedDocuments = myTokenTextSplitter.splitCustomized(documents);
        simpleVectorStore.add(splitedDocuments);

        // 自动补充关键词元信息
        List<Document> enrichedDocuments = myKeywordEnricher.enrichDocuments(splitedDocuments);
        simpleVectorStore.add(documents);

        return simpleVectorStore;
    }
    /**
     * ETL 数据处理流程
     */
//    public void etl(){
//        //从指定的文档抽取文本数据
//        PDFReader pdfReader = new PagePdfDocumentReader("test.pdf");
//                List<Document> documents = pdfReader.read();
//
//
//        MyTokenTextSplitter myTokenTextSplitter = new MyTokenTextSplitter();
//        List<Document> documentList = myTokenTextSplitter.splitDocuments(documents);
//
//        List<Document> documentListSummary = myTokenTextSplitter.enrichDoucmentsBySummary(documentsList);
//
//        vectorStore.write(documentListSummary);
//    }
}